| Conditions | 1 |
| Total Lines | 260 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 21 | constructor(koList, koSelectedItem, koFocusedItem, |
||
| 22 | sItemSelector, sItemSelectedSelector, sItemCheckedSelector, sItemFocusedSelector) |
||
| 23 | { |
||
| 24 | this.list = koList; |
||
| 25 | |||
| 26 | this.listChecked = ko.computed(() => _.filter(this.list(), (item) => item.checked())).extend({rateLimit: 0}); |
||
| 27 | this.isListChecked = ko.computed(() => 0 < this.listChecked().length); |
||
| 28 | |||
| 29 | this.focusedItem = koFocusedItem || ko.observable(null); |
||
| 30 | this.selectedItem = koSelectedItem || ko.observable(null); |
||
| 31 | this.selectedItemUseCallback = true; |
||
| 32 | |||
| 33 | this.itemSelectedThrottle = _.debounce(_.bind(this.itemSelected, this), 300); |
||
| 34 | |||
| 35 | this.listChecked.subscribe((items) => { |
||
| 36 | if (0 < items.length) |
||
| 37 | { |
||
| 38 | if (null === this.selectedItem()) |
||
| 39 | { |
||
| 40 | if (this.selectedItem.valueHasMutated) |
||
| 41 | { |
||
| 42 | this.selectedItem.valueHasMutated(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | else |
||
| 46 | { |
||
| 47 | this.selectedItem(null); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | else if (this.autoSelect() && this.focusedItem()) |
||
| 51 | { |
||
| 52 | this.selectedItem(this.focusedItem()); |
||
| 53 | } |
||
| 54 | }, this); |
||
| 55 | |||
| 56 | this.selectedItem.subscribe((item) => { |
||
| 57 | |||
| 58 | if (item) |
||
| 59 | { |
||
| 60 | if (this.isListChecked()) |
||
| 61 | { |
||
| 62 | _.each(this.listChecked(), (subItem) => { |
||
| 63 | subItem.checked(false); |
||
| 64 | }); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (this.selectedItemUseCallback) |
||
| 68 | { |
||
| 69 | this.itemSelectedThrottle(item); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | else if (this.selectedItemUseCallback) |
||
| 73 | { |
||
| 74 | this.itemSelected(null); |
||
| 75 | } |
||
| 76 | |||
| 77 | }, this); |
||
| 78 | |||
| 79 | this.selectedItem = this.selectedItem.extend({toggleSubscribe: [null, |
||
| 80 | (prev) => { |
||
| 81 | if (prev) |
||
| 82 | { |
||
| 83 | prev.selected(false); |
||
| 84 | } |
||
| 85 | }, (next) => { |
||
| 86 | if (next) |
||
| 87 | { |
||
| 88 | next.selected(true); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | ]}); |
||
| 92 | |||
| 93 | this.focusedItem = this.focusedItem.extend({toggleSubscribe: [null, |
||
| 94 | (prev) => { |
||
| 95 | if (prev) |
||
| 96 | { |
||
| 97 | prev.focused(false); |
||
| 98 | } |
||
| 99 | }, (next) => { |
||
| 100 | if (next) |
||
| 101 | { |
||
| 102 | next.focused(true); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | ]}); |
||
| 106 | |||
| 107 | this.iSelectNextHelper = 0; |
||
| 108 | this.iFocusedNextHelper = 0; |
||
| 109 | this.oContentVisible = null; |
||
| 110 | this.oContentScrollable = null; |
||
| 111 | |||
| 112 | this.sItemSelector = sItemSelector; |
||
| 113 | this.sItemSelectedSelector = sItemSelectedSelector; |
||
| 114 | this.sItemCheckedSelector = sItemCheckedSelector; |
||
| 115 | this.sItemFocusedSelector = sItemFocusedSelector; |
||
| 116 | |||
| 117 | this.sLastUid = ''; |
||
| 118 | this.oCallbacks = {}; |
||
| 119 | |||
| 120 | this.focusedItem.subscribe((item) => { |
||
| 121 | if (item) |
||
| 122 | { |
||
| 123 | this.sLastUid = this.getItemUid(item); |
||
| 124 | } |
||
| 125 | }, this); |
||
| 126 | |||
| 127 | let |
||
| 128 | aCache = [], |
||
| 129 | aCheckedCache = [], |
||
| 130 | mFocused = null, |
||
| 131 | mSelected = null; |
||
| 132 | |||
| 133 | this.list.subscribe((items) => { |
||
| 134 | |||
| 135 | if (isArray(items)) |
||
| 136 | { |
||
| 137 | _.each(items, (item) => { |
||
| 138 | if (item) |
||
| 139 | { |
||
| 140 | const uid = this.getItemUid(item); |
||
| 141 | |||
| 142 | aCache.push(uid); |
||
| 143 | if (item.checked()) |
||
| 144 | { |
||
| 145 | aCheckedCache.push(uid); |
||
| 146 | } |
||
| 147 | if (null === mFocused && item.focused()) |
||
| 148 | { |
||
| 149 | mFocused = uid; |
||
| 150 | } |
||
| 151 | if (null === mSelected && item.selected()) |
||
| 152 | { |
||
| 153 | mSelected = uid; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | }); |
||
| 157 | } |
||
| 158 | }, this, 'beforeChange'); |
||
| 159 | |||
| 160 | this.list.subscribe((aItems) => { |
||
| 161 | |||
| 162 | var |
||
| 163 | oTemp = null, |
||
| 164 | bGetNext = false, |
||
| 165 | aUids = [], |
||
| 166 | mNextFocused = mFocused, |
||
| 167 | bChecked = false, |
||
| 168 | bSelected = false, |
||
| 169 | iLen = 0; |
||
| 170 | |||
| 171 | this.selectedItemUseCallback = false; |
||
| 172 | |||
| 173 | this.focusedItem(null); |
||
| 174 | this.selectedItem(null); |
||
| 175 | |||
| 176 | if (isArray(aItems)) |
||
| 177 | { |
||
| 178 | iLen = aCheckedCache.length; |
||
| 179 | |||
| 180 | _.each(aItems, (oItem) => { |
||
| 181 | |||
| 182 | var sUid = this.getItemUid(oItem); |
||
| 183 | aUids.push(sUid); |
||
| 184 | |||
| 185 | if (null !== mFocused && mFocused === sUid) |
||
| 186 | { |
||
| 187 | this.focusedItem(oItem); |
||
| 188 | mFocused = null; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (0 < iLen && -1 < inArray(sUid, aCheckedCache)) |
||
| 192 | { |
||
| 193 | bChecked = true; |
||
| 194 | oItem.checked(true); |
||
| 195 | iLen -= 1; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!bChecked && null !== mSelected && mSelected === sUid) |
||
| 199 | { |
||
| 200 | bSelected = true; |
||
| 201 | this.selectedItem(oItem); |
||
| 202 | mSelected = null; |
||
| 203 | } |
||
| 204 | }); |
||
| 205 | |||
| 206 | this.selectedItemUseCallback = true; |
||
| 207 | |||
| 208 | if (!bChecked && !bSelected && this.autoSelect()) |
||
| 209 | { |
||
| 210 | if (this.focusedItem()) |
||
| 211 | { |
||
| 212 | this.selectedItem(this.focusedItem()); |
||
| 213 | } |
||
| 214 | else if (0 < aItems.length) |
||
| 215 | { |
||
| 216 | if (null !== mNextFocused) |
||
| 217 | { |
||
| 218 | bGetNext = false; |
||
| 219 | mNextFocused = _.find(aCache, (sUid) => { |
||
| 220 | if (bGetNext && -1 < inArray(sUid, aUids)) |
||
| 221 | { |
||
| 222 | return sUid; |
||
| 223 | } |
||
| 224 | else if (mNextFocused === sUid) |
||
| 225 | { |
||
| 226 | bGetNext = true; |
||
| 227 | } |
||
| 228 | return false; |
||
| 229 | }); |
||
| 230 | |||
| 231 | if (mNextFocused) |
||
| 232 | { |
||
| 233 | oTemp = _.find(aItems, (oItem) => mNextFocused === this.getItemUid(oItem)); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | this.selectedItem(oTemp || null); |
||
| 238 | this.focusedItem(this.selectedItem()); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if ((0 !== this.iSelectNextHelper || 0 !== this.iFocusedNextHelper) && 0 < aItems.length && !this.focusedItem()) |
||
| 243 | { |
||
| 244 | oTemp = null; |
||
| 245 | if (0 !== this.iFocusedNextHelper) |
||
| 246 | { |
||
| 247 | oTemp = aItems[-1 === this.iFocusedNextHelper ? aItems.length - 1 : 0] || null; |
||
| 248 | } |
||
| 249 | |||
| 250 | if (!oTemp && 0 !== this.iSelectNextHelper) |
||
| 251 | { |
||
| 252 | oTemp = aItems[-1 === this.iSelectNextHelper ? aItems.length - 1 : 0] || null; |
||
| 253 | } |
||
| 254 | |||
| 255 | if (oTemp) |
||
| 256 | { |
||
| 257 | if (0 !== this.iSelectNextHelper) |
||
| 258 | { |
||
| 259 | this.selectedItem(oTemp || null); |
||
| 260 | } |
||
| 261 | |||
| 262 | this.focusedItem(oTemp || null); |
||
| 263 | |||
| 264 | this.scrollToFocused(); |
||
| 265 | |||
| 266 | _.delay(() => this.scrollToFocused(), 100); |
||
| 267 | } |
||
| 268 | |||
| 269 | this.iSelectNextHelper = 0; |
||
| 270 | this.iFocusedNextHelper = 0; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | aCache = []; |
||
| 275 | aCheckedCache = []; |
||
| 276 | mFocused = null; |
||
| 277 | mSelected = null; |
||
| 278 | |||
| 279 | }, this); |
||
| 280 | } |
||
| 281 | |||
| 762 |